package a66weding.com.jiehuntong.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.andview.refreshview.XRefreshView;

import java.util.List;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.adapter.MyTaskAdapter;
import a66weding.com.jiehuntong.adapter.MyTaskBAdapter;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.MarriageProposalBean;
import a66weding.com.jiehuntong.beans.MyTaskBean;
import a66weding.com.jiehuntong.beans.MyTaskNewBean;
import a66weding.com.jiehuntong.beans.ReleaseTaskBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.http.UrlDealUtil;
import a66weding.com.jiehuntong.inner.OnItemClickLitener;
import a66weding.com.jiehuntong.pager.IContant;
import a66weding.com.jiehuntong.widgets.SpaceItemDecoration;
/**
 * Created by admin on 2016/7/21.
 * 任务 我发布的任务 我的任务;
 */
public class TaskActivity extends Activity implements OnItemClickLitener,OnNetResultListener,IContant{

    private XRefreshView xRefreshView;
    private RecyclerView recyclerView;
    private MyTaskAdapter myTaskAdapter;

    private  MyTaskBean myTaskBean;
    private List<MyTaskNewBean.DataBean> list ;
    private  int index;
    private TextView title;
    //我发布的任务
    private ReleaseTaskBean releaseTaskBean;
    private List<ReleaseTaskBean.DataBean> relaseTaskDatalist;
    private MyTaskBAdapter  myTaskBAdapter;
    private MarriageProposalBean.DataBean MarriageBean;
    private int flag;
    //判断是否上啦下拉刷新
    private boolean isDownRefresh = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_a);
        index =  getIntent().getIntExtra(MyTaskActivity.WhateOfTask,0);
        initView(index);
        getMyTask();
    }

    private void initView(int index){
        title = (TextView) findViewById(R.id.title_tv);
        initRefreshView(index);
        if (index==1){
             title.setText("我的任务");
        }else {
            title.setText("我发布的任务");
        }
        initData(index);
    }

    public void initData(int index){
        if(index==1){
            getMyTask();
            myTaskAdapter.setOnItemClickLitener(this);
        }else{
            //获取我发布的数据;
            getBData();
            myTaskBAdapter.setOnItemClickLitener(this);
        }
    }
    //初始化上啦刷新
    private void initRefreshView(final int index) {
        xRefreshView = (XRefreshView)findViewById(R.id.activity_task_a_x_view);
        //是否允许下拉刷新
        xRefreshView.setPullRefreshEnable(true);
        // 设置是否可以上拉加载
        xRefreshView.setPullLoadEnable(true);
        // 设置刷新完成以后，headerview固定的时间
        xRefreshView.setPinnedTime(1000);
        // 静默加载模式不能设置footerview
        // 设置支持自动刷新
        xRefreshView.setAutoLoadMore(true);
        recyclerView = (RecyclerView)findViewById(R.id.activity_task_a_recycler);
        recyclerView.setHasFixedSize(true);
        int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.size30);
        //线性布局
        recyclerView.addItemDecoration(new SpaceItemDecoration(spacingInPixels));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        //添加适配器
        if(index==1){
            myTaskAdapter = new MyTaskAdapter(this,null);
            recyclerView.setAdapter(myTaskAdapter);
        }else{
            myTaskBAdapter = new MyTaskBAdapter(this,null);
            recyclerView.setAdapter(myTaskBAdapter);
        }
        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh() {
              isDownRefresh = true;
                if(index ==1){
                    getMyTask();
                }else{
                    getBData();
                }

            }
            @Override
            public void onLoadMore(boolean isSlience) {
                isDownRefresh = false;
                if(index ==1){
                    getMyTask();
                }else{
                    getBData();
                }
            }
        });
    }

    public void back(View view){
        finish();
    }


    @Override
    public void onItemClick(View view, int position) {

        if(index==1){
            //我的任务;
            Intent intent = new Intent(this,JobDetail.class);
            getOkA(myTaskNewBean.getData().get(position));
            intent.putExtra("headpictrue", myTaskNewBean.getData().get(position).getPicture());
            flag = flag1;
            intent.putExtra(JobDetail.TASK_FLAG,MarriageBean);
            intent.putExtra("flag",flag);
            startActivityForResult(intent,1);
        }else if(index == 2){//我发布的任务;
            Intent intent = new Intent(this,JobDetailActivity.class);
            //我发布的任务;
            getOkB(releaseTaskBean.getData().get(position));
            intent.putExtra("headpictrue",""+releaseTaskBean.getData().get(position).getPicture()==null?"":""+releaseTaskBean.getData().get(position).getPicture());
            flag = flag2;
            intent.putExtra(JobDetail.TASK_FLAG,MarriageBean);
            intent.putExtra("flag",flag);
            startActivityForResult(intent,1);
        }

    }

    private MyTaskNewBean myTaskNewBean;
    private List<MyTaskNewBean.DataBean> mytasknewlist;
    @Override
    public void onSucess(SuccessBean sucessbean) {
        if (isDownRefresh){
            xRefreshView.stopRefresh();
        }else{
            xRefreshView.stopLoadMore();
        }
        Object data =  sucessbean.getData();
        if (data instanceof ReleaseTaskBean){
            releaseTaskBean = (ReleaseTaskBean) data;
            switch (releaseTaskBean.getResCode()){
                case "0000":
                    relaseTaskDatalist = releaseTaskBean.getData();
                    if (isDownRefresh){
                        //如果你是下拉刷新,我就情况列表
                        myTaskBAdapter.clear();
                    }
                    myTaskBAdapter.append(relaseTaskDatalist);
                    break;
            }
        }

        if (data instanceof  MyTaskNewBean){
            myTaskNewBean = (MyTaskNewBean) data;
            switch (myTaskNewBean.getResCode()){
                case "0000":
                    mytasknewlist  = myTaskNewBean.getData();
                    if (isDownRefresh){
                        if(myTaskAdapter != null){
                            myTaskAdapter.clear();
                        }
                    }
                    if(myTaskAdapter != null){
                        myTaskAdapter.append(mytasknewlist);
                    }
                    break;
            }
        }
    }

    @Override
    public void onError(ErrorBean errorBean) {

    }

    /**
     * 获取我发布的任务;
     */
    public void getBData(){
        String url  = UrlDealUtil.getRealUrl(RequestCode.RELEASEDTASK);
        HttpRequestBean requestBean = new HttpRequestBean(RequestCode.POSTEQUEST, url, this,ReleaseTaskBean.class, RequestCode.RELEASEDTASK);
        HttpHandleUtil.executeCommonPost(this, requestBean);
    }

    public void getOkB(ReleaseTaskBean.DataBean dataBean){
        MarriageBean = new  MarriageProposalBean.DataBean();
        MarriageBean.setParticipant(dataBean.getParticipant());
        MarriageBean.setIsComplete(dataBean.isIsComplete());
        MarriageBean.setBookTitle(dataBean.getBookTitle());
        MarriageBean.setBookPlace(dataBean.getBookPlace());
        MarriageBean.setDays(dataBean.getDays());
        MarriageBean.setNotice(dataBean.getNotice());
        MarriageBean.setRelationName(dataBean.getRelationName());
        MarriageBean.setBudgetMoney(dataBean.getBudgetMoney());
        MarriageBean.setRemarks(dataBean.getRemarks());
        MarriageBean.setNvc_Name(dataBean.getNvc_Name());
    }

    public void getOkA(MyTaskNewBean.DataBean dataBean){
        MarriageBean = new  MarriageProposalBean.DataBean();
        MarriageBean.setParticipant(dataBean.getParticipant());
        MarriageBean.setIsComplete(dataBean.isIsComplete());
        MarriageBean.setBookTitle(dataBean.getBookTitle());
        MarriageBean.setBookPlace(dataBean.getBookPlace());
        MarriageBean.setDays(dataBean.getDays());
        MarriageBean.setNotice(dataBean.getNotice());
        MarriageBean.setRelationName(dataBean.getRelationName());
        MarriageBean.setBudgetMoney(dataBean.getBudgetMoney());
        MarriageBean.setRemarks(dataBean.getRemarks());
        MarriageBean.setNvc_Name(dataBean.getNvc_Name());
        MarriageBean.setMemberID(dataBean.getMemberID());
    }

    /**
     * POST Wedding/return_MyTask?Token={Token}&i_id={i_id}
     我的任务
     */
    private void getMyTask(){
        String url = UrlDealUtil.getRealUrl( RequestCode.RETURN_MYTASK);
        HttpRequestBean requestBean = new HttpRequestBean(RequestCode.POSTEQUEST, url, this, MyTaskNewBean.class, RequestCode.RETURN_MYTASK);
        HttpHandleUtil.executeCommonPost(this, requestBean);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case 1:
                //重新获取数据;
                getMyTask();
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

}
